//
//  FeedbackViewController.m
//  ZhongKeXinHeater
//
//  Created by Kendama on 2017/6/1.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "FeedbackViewController.h"
#import "FIDPlaceholderTextView.h"
#import "PublicFeedbackApi.h"

@interface FeedbackViewController ()

@property(nonatomic,strong) FIDPlaceholderTextView *textView;
@property(nonatomic,strong) UIButton *submitButton;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)refreshBaseControlValue {
    
    [self setLeftButtonImage:[UIImage imageNamed:@"nav_icon_back"]];
    [self setNavigationTitle:@"意见反馈" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
    
    [self masLayoutSubviews];
    
    
}

#pragma mark -- 事件
- (void)didClickSubmitButton {
    NSString *suggest = self.textView.text;
    if ([suggest isEqualToString:@"请写下您的宝贵意见"]) {
        [SVProgressHUD showErrorWithStatus:@"请输入您的意见"];
        return;
    }
    
    if (suggest.length == 0 ) {
        
        [SVProgressHUD showInfoWithStatus:@"请输入您的意见" maskType:SVProgressHUDMaskTypeBlack];
        return;
    }
    
    if (![suggest isChineseCharacter]) {
        [SVProgressHUD showErrorWithStatus:@"建议只能输入中文、字母或数字"];
        return;
    }
  
    if ([NSString isContainsTwoEmoji:suggest] ) {
        [SVProgressHUD showInfoWithStatus:@"意见不能使用表情"];
        return;
    }
    
    NSString *feedback = [suggest stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    PublicFeedbackApi *api = [[PublicFeedbackApi alloc]initWithDic:@{@"content":feedback,@"type_id":@"1121"}];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSDictionary *dic = [api responseJSONObject];
        if ([dic[@"errcode"] intValue] == 0) {
            [SVProgressHUD showInfoWithStatus:@"感谢您的反馈！"];
            self.textView.text = @"";
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [SVProgressHUD showInfoWithStatus:dic[@"errmsg"]];
        }
    } failure:nil];
}



#pragma mark -- 懒加载
- (FIDPlaceholderTextView *)textView
{
    if(!_textView)
    {
        UIView *superView = self.view;
        _textView = [[FIDPlaceholderTextView alloc]init];
        _textView.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        _textView.layer.cornerRadius = 5;
        _textView.layer.borderWidth = 0.5;
        [_textView.layer setMasksToBounds:YES];
        _textView.textColor = [UIColor colorWithHexString:@"6F6F6F"];
        _textView.font = [UIFont systemFontOfSize:px1080Width(50)];
        _textView.backgroundColor = [UIColor whiteColor];
        [superView addSubview:_textView];
    }
    return _textView;
}

- (UIButton *)submitButton
{
    if(!_submitButton)
    {
        _submitButton = [[UIButton alloc]init];
        [_submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [_submitButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _submitButton.layer.cornerRadius = 20;
        _submitButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [_submitButton addTarget:self action:@selector(didClickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_submitButton];
    }
    return _submitButton;
}

#pragma mark -- 初始化
- (void)masLayoutSubviews{
    __weak UIView *superView = self.view;
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(px1920Height(48)+64);
        make.left.mas_equalTo(superView).with.offset(px1080Width(60));
        make.right.mas_equalTo(superView).with.offset(-px1080Width(60));
        make.height.mas_equalTo(px1920Height(300));
    }];
    
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).with.offset(px1920Height(84));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
}

@end
